package com.note.simpleadmin.module.authc.controller;

import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.vo.Msg;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/***
 *@ClaseName:LoginController
 *
 *@Decription:用户登录
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/26 0026 20:49
 */
@Controller
@RequestMapping(value = "authc")
public class LoginController {
    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);
    @RequestMapping(value = "login")
    public String login()
    {
        return "login";
    }
    @RequestMapping(value = "ajax_login")
    @ResponseBody
    public Msg ajax_login(String username, String password) {
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        Msg msg=new Msg();
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            logger.info("对用户[" + username + "]进行登录验证..验证未通过,未知账户");
            msg.setMsg("未知账户");
            msg.setErrorCode(ErrorCode.ERROR_1000);
        } catch (IncorrectCredentialsException ice) {
            logger.info("对用户[" + username + "]进行登录验证..验证未通过,错误的凭证");
            msg.setMsg("密码不正确");
            msg.setErrorCode(ErrorCode.ERROR_1000);
        } catch (LockedAccountException lae) {
            logger.info("对用户[" + username + "]进行登录验证..验证未通过,账户已锁定");
            msg.setMsg("账户已锁定");
            msg.setErrorCode(ErrorCode.ERROR_1000);
        } catch (ExcessiveAttemptsException eae) {
            logger.info("对用户[" + username + "]进行登录验证..验证未通过,错误次数过多");
            msg.setMsg("用户名或密码错误次数过多");
            msg.setErrorCode(ErrorCode.ERROR_1000);
        } catch (AuthenticationException ae) {
            // 通过处理Shiro的运行时AuthenticationException就可以控制用户登录失败或密码错误时的情景
            logger.error(ae.getMessage());
            msg.setMsg("用户名或密码不正确");
            msg.setErrorCode(ErrorCode.ERROR_1000);
        }
        catch (Exception ae) {
            logger.error(ae.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        if (currentUser.isAuthenticated()) {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("ok");
            msg.setUrl("/index");
        }
        return msg;
    }
    /**
     * 作者： xubingbing 描述：系统退出 创建日期：2017年4月19日
     * @return
     */
    @RequestMapping(value = "logOut")
    public String logOut() {
        try {
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "login";
        }
        return "redirect:/authc/login";
    }
}
